//
//  ViewController.m
//  QZTabBarContentView
//
//  Created by admin on 2017/8/10.
//  Copyright © 2017年 QZ. All rights reserved.
//


#import "LCBeautifulEssayListTableViewController.h"
#import "LCHomeTableViewController.h"
#import "UIViewController+LCLoadShop.h"
#import "LCViewController+LCLoadData.h"

@interface LCBeautifulEssayListTableViewController ()
<QZTabBarContentViewDataSource,QZTabBarContentViewDelegate>

@property (strong, nonatomic) LCHomeChannelListsTableViewController *homeChannelListsTableViewController;//音频

@end

@implementation LCBeautifulEssayListTableViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.array.count == 1) {
        [self loadChannelData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupNVUI];
    self.tabBarContentView.dataSource = self;
    self.tabBarContentView.delegate = self;
    [self loadChannelData];//读取标签数据 
    [self LCResetTabBarClick];
    /**增加侧滑返回,无其他作用*/
    [LCUtils lcAddSideLeftView:self.view];

}
//回到第一页
-(void)LCResetTabBarClick {
//    //联动内容
//    UIButton *selectedItemButton = [self.tabBarContentView.tabBarItems firstObject];
//    [self.tabBarContentView actionSelectItemButton:selectedItemButton];
}

///解决自动的向下偏移64像素
- (void)setupNVUI {
    self.title = @"文章";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;//设置为不半透明也可以解决灰色的tabBar问题
    QZTabBarContentView *tabBarContentView = [[QZTabBarContentView alloc] init];
    _tabBarContentView = tabBarContentView;
    self.tabBarContentView.tabBarTitleStyle = 3;
    [self.view addSubview:tabBarContentView];
    [tabBarContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(0);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        }else{
            make.top.equalTo(self.view.mas_top).offset(0);
            make.bottom.equalTo(self.view.mas_bottom).offset(0);
        }
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
    }];
}

#pragma mark - QZTabBarContentViewDelegate -
-(UIColor *)highlightColorForTabBarItemInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    return RGBColor(235, 82, 74);
}
-(CGFloat)heightForTabBarInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    return 44;
}
-(UIColor *)colorForTabBarItemTextInTabBarContentView:(QZTabBarContentView *)tabBarContentView{
    return RGBColor(140, 140, 140);
}
#pragma mark - QZTabBarContentViewDataSource -
/**
 放回当前控件需要多少个选项
 @param tabBarContentView 当前控件
 @return 返回选项个数
 */
- (NSInteger)numberOfItemsInTabBarContentView:(QZTabBarContentView *)tabBarContentView {
    return _array.count;
}
/**
 提供选项的标题
 
 @param tabBarContentView 当前控件
 @param index 标题索引
 @return 返回标题内容
 */
- (NSString *)tabBarContentView:(QZTabBarContentView *)tabBarContentView titleForItemAtIndex:(NSInteger)index {
    return [NSString stringWithFormat:@"%@",_array[index].title];
    //    return [NSString stringWithFormat:@"item %zd",index];
}

/**
 提供内容视图
 @param tabBarContentView 当前控件
 @param index 标题索引
 @return 内容视图
 */
- (UIView *)tabBarContentView:(QZTabBarContentView *)tabBarContentView contentViewAtIndex:(NSInteger)index {
    
    NSString *cid = _array[index].id;
    self.homeChannelListsTableViewController = [[LCHomeChannelListsTableViewController alloc] init];
    self.homeChannelListsTableViewController.cid = cid;
    //一定要加下面这句  不然不能push
    [self addChildViewController:self.homeChannelListsTableViewController];
    return self.homeChannelListsTableViewController.view;
}

#pragma mark - QZTabBarContentViewDelegate -

/**
 返回一个当前选项选中的视图

 @param tabBarContentView 当前控件
 @return 选中的视图
 */
- (UIView *)highlightViewForTabBarItemInTabBarContentView:(QZTabBarContentView *)tabBarContentView {
    UIView *view = [UIView new];
    
    UIView *line = [UIView new];
    line.backgroundColor = redUIColor;
    // 设置圆角大小
    line.radius = 1.25;
    [view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(view).offset(1);
        make.centerX.equalTo(view);
        make.width.mas_equalTo(55);
        make.height.mas_equalTo(2.5);
    }];

    return view;
}
/**
 当选中状态发生改变时回调

 @param tabBarContentView 当前控件
 @param index 选项下标
 */
- (void)tabBarContentView:(QZTabBarContentView *)tabBarContentView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"index:%ld",(long)index);
    NSString *cid = _array[index].id;
    NSLog(@"cid = %@", cid);
    [[NSUserDefaults standardUserDefaults] setObject:cid forKey:@"didSelectChannelAtCid"];

}
- (void)loadChannelData{
    YYCache *yyCache=[YYCache cacheWithName:@"LCHomeChannelName"];
    if ([yyCache containsObjectForKey:@"keyHomeChannel"]) {
        id vuale = [yyCache objectForKey:@"keyHomeChannel"];
        self.array = [NSArray yy_modelArrayWithClass:[ChannelModel class] json:vuale];
        [self.tabBarContentView reloadData];
    }
    [ChannelModel getHomeCateWithSuccBlock:^(NSArray<ChannelModel *> *modelArr) {
        if (self.array.count == modelArr.count) {
            BOOL isOK = NO;
            for (NSInteger i = 0; i < modelArr.count; i++) {
                if ([self.array[i].title isEqualToString:modelArr[i].title] && [self.array[i].id isEqualToString:modelArr[i].id]) {
                    isOK = YES;
                }else{
                    isOK = NO;
                    break;
                }
            }
            if (isOK == NO/**假如和缓存的标签中title且id不一致就刷新*/) {
                self.array = modelArr;
                [self.tabBarContentView reloadData];
            }
        }else{
            self.array = modelArr;
            [self.tabBarContentView reloadData];
        }
    }];
}


@end
